{
LANG : PASCAL
PROB : fence6
}

program FENCE6;
const
  fi = 'fence6.in';
  fo = 'fence6.out';
  max = 100;
  oo = 15000;
var
  a : array[1..max, 1..max] of integer;
  c : array[1..max, 1..2, 1..8] of integer;
  d : array[1..max] of integer;
  l, p : array[1..max, 1..2] of integer;
  n, m, best : integer;

procedure inp;
var
  i, j, k : integer;
begin
  read(n);
  for i := 1 to n do
  begin
    read(k, d[k], l[k, 1], l[k, 2]);
    for j := 1 to l[k, 1] do read(c[k, 1, j]);
    for j := 1 to l[k, 2] do read(c[k, 2, j]);
  end;
end;

procedure init;
var
  i, j, k, q, r : integer;
  seen : boolean;
begin
  fillchar(p, sizeof(p), 0);
  m := 0;
  for i := 1 to n do
    for j := 1 to 2 do
      if p[i, j] = 0 then
      begin
        inc(m);
        p[i, j] := m;
        for k := 1 to l[i, j] do
        begin
          q := c[i, j, k];
          seen := false;
          for r := 1 to l[q, 1] do
            if c[q, 1, r] = i then
            begin
              seen := true;
              break;
            end;
          if seen then p[q, 1] := m
          else p[q, 2] := m;
        end;
      end;
end;

procedure creat_graph;
var
  i : integer;
begin
  fillchar(a, sizeof(a), 0);
  for i := 1 to n do
  begin
    a[p[i, 1], p[i, 2]] := d[i];
    a[p[i, 2], p[i, 1]] := d[i];
  end;
end;

function dijkstra(s, f : integer) : integer;
var
  free : array[1..max] of boolean;
  d : array[1..max] of integer;
  i, u, v, min : integer;
begin
  fillchar(free, sizeof(free), true);
  for i := 1 to m do d[i] := oo;
  d[s] := 0;
  repeat
    min := oo;
    for i := 1 to m do
      if free[i] and (d[i] < min) then
      begin
        min := d[i];
        u := i;
      end;
    if min = oo then break;
    free[u] := false;
    for v := 1 to m do
      if free[v] and (a[u, v] > 0) and (d[v] > d[u] + a[u, v]) then
        d[v] := d[u] + a[u, v];
  until false;
  dijkstra := d[f];
end;

procedure solve;
var
  u, v, g, temp : integer;
begin
  best := oo;
  for u := 1 to m do
    for v := u+1 to m do
      if a[u, v] > 0 then
      begin
        temp := a[u, v];
        a[u, v] := 0;
        g := dijkstra(u, v);
        if best > g + temp then best := g + temp;
        a[u, v] := temp;
      end;
end;

procedure process;
begin
  creat_graph;
  solve;
end;

begin
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);
  inp;
  init;
  process;
  writeln(best);
  close(output);
  close(input);
end.